package com.sls.web.manager.recharge.service.impl;

import java.util.UUID;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.SupplierDenomination;
import com.sls.web.manager.recharge.querybean.SupplierDenominationQuery;
import com.sls.web.manager.recharge.service.SupplierDenominationService;
import com.sls.web.manager.socket.SuperService;

@Service("supplierDenominationService")
public class SupplierDenominationServiceImpl extends SuperService implements
		SupplierDenominationService {

	@Override
	public boolean add(SupplierDenomination s, UserInfoVO userinfo)
			throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.INSERT_SUPPLIER_DEOMINATION);
		head.setUserId(userinfo.getUserId());
		s.setUpdateOperator(userinfo.getUserName());
		SocketDTO dto = super.send(head, s);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());

	}

	@Override
	public Pagination<?> queryList(SupplierDenominationQuery rQuery,
			UserInfoVO userinfo) throws Exception {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_SUPPLIER_DEOMINATION_LIST);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, rQuery);
		String body = dto.getBody();
		Pagination<SupplierDenomination> supplierDenomination = gson.fromJson(
				body, new TypeToken<Pagination<SupplierDenomination>>() {
				}.getType());

		return supplierDenomination;
	}

	@Override
	public boolean delete(String id, UserInfoVO userinfo) throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DELETE_SUPPLIER_DEOMINATION);
		SupplierDenomination su = new SupplierDenomination();
		su.setId(id);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, su);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public SupplierDenomination queryById(String id, UserInfoVO userinfo)
			throws Exception {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_SUPPLIER_DEOMINATION_BY_ID);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SupplierDenomination sa = new SupplierDenomination();
		sa.setId(id);
		SocketDTO dto = super.send(head, sa);
		String body = dto.getBody();
		SupplierDenomination supplierDenomination = gson.fromJson(body,
				SupplierDenomination.class);
		return supplierDenomination;
	}

	@Override
	public boolean update(SupplierDenomination sa, UserInfoVO userinfo)
			throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.UPDATE_SUPPLIER_DEOMINATION);
		sa.setUpdateOperator(userinfo.getUserId());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, sa);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public boolean updateRechargeById(String id, UserInfoVO userinfo)
			throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setUserId(userinfo.getUserId());
		head.setServiceCode(Constants.UPDATE_SUPPLIER_DEOMINATION_BY_ID);
		SupplierDenomination sa = new SupplierDenomination();
		sa.setId(id);
		sa.setUpdateOperator(userinfo.getUserId());
		SocketDTO dto = super.send(head, sa);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

}
